<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Garbage collected objects</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_gc_object.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Garbage collected objects </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>Reference counting as memory management has a drawback in that it is difficult to detect circular references when determining dead objects. AngelScript allows the application to register types with special behaviours to support the garbage collection for detecting circular references. These behaviours make the class a bit more complex, but you should only have to register them for a few types, e.g. generic container classes.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Registering the garbage collected reference type</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a29c6c087c8c5b5cdb6271cfd161cc5a6" title="Registers a new object type.">RegisterObjectType</a>(<span class="stringliteral">&quot;ref&quot;</span>, 0, <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa9450e038342b36c745858d2e5ae4b861" title="A reference type.">asOBJ_REF</a> | <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aacc1d835f9c25043cef86026a4aa6a470" title="A garbage collected type. Only valid for reference types.">asOBJ_GC</a>); assert( r &gt;= 0 );
</pre></div><p>The difference between the garbage collected and non-garbage collected types is in the addref and release behaviours, the class constructor, and the extra support behaviours.</p>
<dl class="see"><dt><b>See also:</b></dt><dd>The <a class="el" href="doc_addon_dict.html">dictionary</a> add-on for an example of a garbage collected object</dd></dl>
<h2><a class="anchor" id="doc_reg_gcref_1"></a>
GC support behaviours</h2>
<p>The GC determines when objects should be destroyed by counting the references it can follow for each object. If the GC can see all references that points to an object, it knows that the object is part of a circular reference. If all the objects involved in that circular reference have no outside references it means that they should be destroyed.</p>
<p>The process of determining the dead objects uses the first for of the behaviours below, while the destruction of the objects is done by forcing the release of the object's references.</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> CGCRef::SetGCFlag()
{
    <span class="comment">// Set the gc flag as the high bit in the reference counter</span>
    refCount |= 0x80000000;
}

<span class="keywordtype">bool</span> CGCRef::GetGCFlag()
{
    <span class="comment">// Return the gc flag</span>
    <span class="keywordflow">return</span> (refCount &amp; 0x80000000) ? <span class="keyword">true</span> : <span class="keyword">false</span>;
}

<span class="keywordtype">int</span> CGCRef::GetRefCount()
{
    <span class="comment">// Return the reference count, without the gc flag</span>
    <span class="keywordflow">return</span> (refCount &amp; 0x7FFFFFFF);
}

<span class="keywordtype">void</span> CGCRef::EnumReferences(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine)
{
    <span class="comment">// Call the engine::GCEnumCallback for all references to other objects held</span>
    engine-&gt;<a class="code" href="classas_i_script_engine.html#a58ceeafd780dea3543e0ede4106199fd" title="Used by the garbage collector to enumerate all references held by an object.">GCEnumCallback</a>(myref);
}

<span class="keywordtype">void</span> CGCRef::ReleaseAllReferences(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine)
{
    <span class="comment">// When we receive this call, we are as good as dead, but</span>
    <span class="comment">// the garbage collector will still hold a references to us, so we</span>
    <span class="comment">// cannot just delete ourself yet. Just free all references to other</span>
    <span class="comment">// objects that we hold</span>
    <span class="keywordflow">if</span>( myref )
    {
        myref-&gt;Release();
        myref = 0;
    }
}

<span class="comment">// Register the GC support behaviours</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5aadbad474a338c3a0fe6e90df679bb2e6" title="(GC) Set GC flag">asBEHAVE_SETGCFLAG</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,SetGCFlag), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5abfce2539609e667f15b24bbc8551c7b7" title="(GC) Get GC flag">asBEHAVE_GETGCFLAG</a>, <span class="stringliteral">&quot;bool f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,GetGCFlag), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5af998529f8ea1e54567997b8fb2867640" title="(GC) Get reference count">asBEHAVE_GETREFCOUNT</a>, <span class="stringliteral">&quot;int f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,GetRefCount), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a08ccf78a37567b5dd192ff5d95c6667b" title="(GC) Enumerate held references">asBEHAVE_ENUMREFS</a>, <span class="stringliteral">&quot;void f(int&amp;in)&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,EnumReferences), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a4275ebe0b4852f2d4a10d4d9db333fe9" title="(GC) Release all references">asBEHAVE_RELEASEREFS</a>, <span class="stringliteral">&quot;void f(int&amp;in)&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,ReleaseAllReferences), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
</pre></div><h2><a class="anchor" id="doc_reg_gcref_2"></a>
Factory for garbage collection</h2>
<p>Whenever a garbage collected class is created, the garbage collector must be notified of it's existence. The easiest way of doing that is to have the factory behaviour, or the class constructor call the <code>NotifyGarbageCollectorOfNewObject()</code> method on the engine when initializing the class.</p>
<div class="fragment"><pre class="fragment">CGCRef *GCRef_Factory()
{
    <span class="comment">// Create the object and then notify the GC of its existence</span>
    CGCRef *obj = <span class="keyword">new</span> CGCRef();
    <span class="keywordtype">int</span> typeId = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad1f6fecb0f53fd7966736b01f65c3dcb" title="Returns a type id by declaration.">GetTypeIdByDecl</a>(<span class="stringliteral">&quot;gc&quot;</span>);
    engine-&gt;<a class="code" href="classas_i_script_engine.html#a8ea9a6f3ed9f69a5c4e07c87281117c0" title="Notify the garbage collector of a new object that needs to be managed.">NotifyGarbageCollectorOfNewObject</a>(obj, typeId);
    <span class="keywordflow">return</span> obj;
}
</pre></div><p>You may want to consider caching the typeId, so that it doesn't have to be looked up through the relatively expensive call to GetTypeIdByDecl every time an object of this type is created.</p>
<p>Note, if you create objects of this type from the application side, you must also notify the garbage collector of its existence, so it's a good idea to make sure all code use the same way of creating objects of this type.</p>
<h2><a class="anchor" id="doc_reg_gcref_3"></a>
Addref and release for garbage collection</h2>
<p>For garbage collected objects it is important to make sure the AddRef and Release behaviours clear the GC flag. Otherwise it is possible that the GC incorrectly determine that the object should be destroyed.</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> CGCRef::AddRef()
{
    <span class="comment">// Clear the gc flag and increase the reference counter</span>
    refCount = (refCount&amp;0x7FFFFFFF) + 1;
}

<span class="keywordtype">void</span> CGCRef::Release()
{
    <span class="comment">// Clear the gc flag, decrease ref count and delete if it reaches 0</span>
    refCount &amp;= 0x7FFFFFFF;
    <span class="keywordflow">if</span>( --refCount == 0 )
        <span class="keyword">delete</span> <span class="keyword">this</span>;
}

<span class="comment">// Registering the addref/release behaviours</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a1dfa5b72ad69a7bf70636d4fcb1b1d84" title="AddRef.">asBEHAVE_ADDREF</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,AddRef), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a7134ce13c81967191af401a1e5170a0c" title="Release.">asBEHAVE_RELEASE</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,Release), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );
</pre></div> </div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
